package com.xxx.shetuan3.service.impl;

import com.xxx.shetuan3.dao.UserDao;
import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.pojo.StudentAndOrganization;
import com.xxx.shetuan3.pojo.User;
import com.xxx.shetuan3.service.OrganizationService;
import com.xxx.shetuan3.service.StudentAndOrganizationService;
import com.xxx.shetuan3.service.StudentService;
import com.xxx.shetuan3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentAndOrganizationService studentAndOrganizationService;
    @Autowired
    private OrganizationService organizationService;

    @Override
    public User findUserByUserId(Integer userId) {
        User res = userService.findUserByUserId(userId);
        return res;
    }

    @Override
    public boolean updateStudent(User user) {
        User user1 = userService.findUserByUserId(user.getUserId());
        if(user1==null){
            throw new ApiException("未找到用户");
        }else{
            boolean res = userService.updateUser(user);
            if(res){
                return true;
            }else{
                throw new ApiException("更新失败");
            }
        }
    }

    @Override
    public boolean joinOrganization(StudentAndOrganization studentAndOrganization) {
        boolean res = studentAndOrganizationService.saveStudentAndOrganization(studentAndOrganization);
        return res;
    }

    @Override
    public List<StudentAndOrganization> queryAllUnacceptedOrganizations(Integer studentId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationService.findUnacceptedOrganizations(studentId,  start, size);
        return studentAndOrganizationList;
    }

    @Override
    public List<Organization> queryAllJoinedOrganizations(Integer studentId, Integer start, Integer size) {
        List<StudentAndOrganization> studentAndOrganizationList = studentAndOrganizationService.findOrganizations(studentId,start,size);
        List<Organization> result = new ArrayList<>();
        for(StudentAndOrganization studentAndOrganization:studentAndOrganizationList){
            Integer organizationId = studentAndOrganization.getOrganizationId();
            Organization organization1 = organizationService.findOrganizationById(organizationId);
            result.add(organization1);
        }
        return result;
    }

    @Override
    public List<Organization> queryAllOrganizations(Integer start, Integer size) {
        List<Organization> organizationList = organizationService.queryPage(start, size);
        return organizationList;
    }

    @Override
    public boolean exitOrganization(Integer studentId, Integer organizationId) {
        StudentAndOrganization studentAndOrganization1 = studentAndOrganizationService.findStudentAndOrganizationByStudentIdAndOrganizationId(studentId, organizationId);
        boolean res = studentAndOrganizationService.deleteStudentAndOrganizationById(studentAndOrganization1.getId());
        if(res){
            return true;
        }else{
            throw new ApiException("退出失败");
        }
    }

}
